package com.ysh.xiaomi.web.servelt;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ysh.xiaomi.service.OrderService;
import com.ysh.xiaomi.service.impl.OrderServiceImpl;
import com.ysh.xiaomi.utils.StringConstans;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 袁胜虎 on 2022/8/19 11:42
 */
public class ResultServlet {
    ObjectMapper objectMapper = new ObjectMapper();
    OrderService orderService = new OrderServiceImpl();
    public  String defaultMethod(HttpServletRequest request, HttpServletResponse response){
        //拿到支付结果
        //根据结果来更新订单的状态
        //更新哪个订单?需要知道订单号,订单号就在参数中
        String resultJson = request.getParameter("result");
        //上面的数据是一个json字符串,需要解析
        try {
            Map map = objectMapper.readValue(resultJson, Map.class);
            //在上面的map中还有一个result作为key,对应的支付结果
            Map payResult = (Map) map.get("result");
            //在支付结果的map中有一个out_trade_no key保存的是我们的订单号
            String  oid = (String) payResult.get("out_trade_no");
            //根据订单号改成已支付
            orderService.updateOrderStatus(oid,"1");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return StringConstans.REDIRECT + "/order?method=show";
    }
}
